
class ReservesController < ApplicationController

  before_action :init_reserve

  def new
    @reserve = Reserve.new
  end

  def create
    @reserve = Reserve.new(params.require(:reserve).permit!)

    user = unless current_user
             pending_user = User.find_by_mobile params[:user][:mobile]
             if pending_user
               #if sms_code_valid?(pending_user)
                 pending_user
               #else
                 #fork_user = User.new
                 #fork_user.errors.add(:sms_code, "手机验证码错误")
                 #fork_user
               #end
             else
               create_user
             end
           else
             current_user
           end
    if user.nil? || user.new_record?
      flash[:error] = generate_flash(user, "用户信息错误")
      return render action: :new
    end
    @reserve.user_id = user.id
    @reserve.cellphone = params[:user][:mobile]
    address = create_address(@reserve)
    @reserve.address_id = address.id
    if @reserve.save
      login_as(user) unless current_user
      if generate_payment_direct? @reserve
        order = Order.create_order_from_reserving! user, address, @reserve.id
        payment = Payment.create_or_update_payment!(current_user, order, Payment::PayType::ALIPAY_PC_DIRECT)
        PaymentService.do_pay payment

        if request.xhr?
          render_success("预约成功", data: callback_payments_path(out_trade_no: payment.payment_no, result: "success"))
        else
          redirect_to callback_payments_path out_trade_no: payment.payment_no, result: "success"
        end
      else
        if request.xhr?
          render_success('预约成功', data: new_order_path(reserve_id: @reserve.id))
        else
          redirect_to controller: :orders, action: :new, reserve_id: @reserve.id
        end
      end
    else
      if request.xhr?
        render_fail("预约出错")
      else
        flash[:error] = generate_flash(@reserve, "预约信息错误")
        render action: :new
      end
    end

  end

  private

  def sms_code_valid? user
    SmsService.get_valid_sms_code(user) == params[:user][:sms_code]
  end

  def create_address reserve
    Address.create(
      user_id: reserve.user_id,
      address_type: Address::AddressType::User,
      contact_name: reserve.contact_name,
      cellphone: params[:user][:mobile],
      street_id: params[:address][:street_id],
      address: reserve.address_desc.present? ? reserve.address_desc : Address::AddressType::NONE).destroy!
  end

  def create_user
    user = User.new(params.require(:user).permit!)
    user.term_of_service = true
    #user.require_sms_code = true
    #user.valid_sms_code = SmsService.get_valid_sms_code(user)
    auto_generated_pwd = RandomCode.generate_password
    user.password = auto_generated_pwd
    user.password_confirmation = user.password
    user.auto_register = true
    if user.save
      SmsService.auto_register_account(user.mobile, user.mobile, auto_generated_pwd)
      user
    else
      nil
    end
  end

  def generate_payment_direct? reserve
    reserve.survey_fee.amount <= 0
  end

  def init_reserve
    @survey_fees = SurveyFee.all
    @franchisers = OuterApiService.get_franchisers
  end

end
